<?php
/**
 * 公共自定义函数
 * User
 * Date
 * Time
 */
use Carbon\Carbon;

if (!function_exists('getrate')) {
    function getrate($a, $b)
    {
        $c = 0;
        $b == 0 ? $c = 0 : $c = number_format($a/$b, 4) * 100;
        return number_format($c, 2);
    }
}
if (!function_exists('getAvg')) {
    function getAvg($a, $b)
    {
        return $b > 0 ? intval($a / $b) : '-';
    }
}
if (!function_exists('getMonthDataArr')) {
    function getMonthDateArr($bt, $et, $format = 'Y-m-d')
    {
        $date = [];
        while (strtotime($bt) <= strtotime($et)) {
            $date[] = date($format, strtotime($bt));
            $bt = date('Y-m-d', strtotime($bt.' +1 day'));
        }
        return $date;
    }
}
if (!function_exists('getMonthDataArrByMonth')) {
    function getMonthDay($month) {
        $bt = date('Y-m-01', strtotime($month));
        $et = date('Y-m-d', strtotime($month.' +1 month - 1 day'));
        $date_arr = getMonthDateArr($bt, $et, 'd');
        foreach ($date_arr as &$d) {
            $d = intval($d);
        }
        return $date_arr;
    }
}
if (!function_exists('getNameByArray')) {
    function getNameByArray($id, $arr)
    {
        foreach ($arr as $av) {
            if ($av['id'] == $id) {
                return $av['name'];
            }
        }
        return '-';
    }
}

if (!function_exists('inMonth')) {
    function inMonth($time, $month, $year)
    {
        $ft = strtotime($year.'-'.$month.'-01 00:00:00');
        $lt = strtotime(date('Y-m-d', strtotime('last day of', $ft)).' 23:59:59');
        $time = strtotime($time);
        if ($time >= $ft && $time <= $lt) {
            return true;
        }
        return false;
    }
}

if (!function_exists('inBtEt')) {
    function inBtEt($time, $bt, $et)
    {
        $ft = strtotime($bt.' 00:00:00');
        $lt = strtotime($et.' 23:59:59');
        $time = strtotime($time);
        if ($time >= $ft && $time <= $lt) {
            return true;
        }
        return false;
    }
}

if (!function_exists('lastMonth')) {
    function lastMonth($count = 24)
    {
        $arr = [];
        $date = date('Y-m', time()).'-01';
        $i = 0;
        while (count($arr) <= $count) {
            $year = date('Y', strtotime($date.' -'.$i.' month'));
            $month = date('m', strtotime($date.' -'.$i.' month'));
            $arr[$year.'年'.$month.'月'] = $year.'-'.$month;
            $i++;
        }
        return $arr;
    }
}

if (!function_exists('buildKeyValueArray')) {
    function buildKeyValueArray($arr, $key = 'name')
    {
        $Arr = [];
        foreach ($arr as $k => $v) {
            $Arr[$v['id']] = $v[$key];
        }
        return $Arr;
    }
}

//建立id为key的数组无待定
if (!function_exists('buildKeyArray')) {
    function buildKeyArray($arr, $key = 'id')
    {
        $Arr = [];
        foreach ($arr as $k => $v) {
            $Arr[$v[$key]] = $v;
        }
        return $Arr;
    }
}

if (!function_exists('buildKeyArrayArr')) {
    function buildKeyArrayArr($arr, $key = 'id', $key2 = 'name')
    {
        $Arr = [];
        foreach ($arr as $k => $v) {
            $Arr[$v[$key]][] = $v;
        }
        return $Arr;
    }
}

if (!function_exists('getBeforeTime')) {
    function getFirstTime($arr)
    {
        if (is_array($arr) || count($arr) == 0) {
            return date('Y-m-d H:i:s', 0);
        }
        $t = strtotime($arr[0]);
        foreach ($arr as $tv) {
            if (strtotime($tv) < $t) {
                $t = strtotime($tv);
            }
        }
        return date('Y-m-d H:i:s', $t);
    }
}

if (!function_exists('getLastTime')) {
    function getLastTime($arr)
    {
        if (is_array($arr) || count($arr) == 0) {
            return date('Y-m-d H:i:s', 0);
        }
        $t = strtotime($arr[0]);
        foreach ($arr as $tv) {
            if (strtotime($tv) > $t) {
                $t = strtotime($tv);
            }
        }
        return date('Y-m-d H:i:s', $t);
    }
}

if (!function_exists('arraySort')) {
    function arraySort($array, $keys, $sort = 'asc')
    {
        $temp = [];
        foreach ($array as $rkk => $rvv) {
            $temp[$rkk] = $rvv[$keys];
        }
        if ($sort == 'asc') {
            array_multisort($temp, SORT_ASC, $array);
        } else {
            array_multisort($temp, SORT_DESC, $array);
        }
        return $array;
    }
}

if (!function_exists('arraySortTwo')) {
    function arraySortTwo($array, $keys, $sort = 'asc')
    {
        $temp = [];
        $keyArr = explode('.', $keys);
        foreach ($array as $rkk => $rvv) {
            $value = $rvv[$keyArr[0]][$keyArr[1]] ?? 0;
            $temp[$rkk]['value'] = $value;
            $temp[$rkk]['key'] = $rkk;
        }
        $temp = arraySort($temp, 'value', $sort);
        $r = [];
        foreach ($temp as $k => $v) {
            $r[$v['key']] = $array[$v['key']];
        }
        return $r;
    }
}

if (!function_exists('lastCycle')) {
    function lastCycle($beginDate, $endDate, $days = 7)
    {
        $diff_days = Carbon::parse($endDate)->diffInDays(Carbon::parse($beginDate)) + 1;
        //如果小于7天则-7，大于7天则看上一个周期
        $before_beginDate = date('Y-m-d', strtotime($beginDate.' -'.$days.' days'));
        $before_endDate = date('Y-m-d', strtotime($endDate.' -'.$days.' days'));
        if ($diff_days > $days) {
            $sunday_week = date('Y-m-d', strtotime('Sunday', strtotime($endDate)));
            $diff_days_sunday = Carbon::parse($sunday_week)->diffInDays(Carbon::parse($endDate));
            $diff_days += $diff_days_sunday;
            $before_beginDate = date('Y-m-d', strtotime($beginDate.' -'.$diff_days.' days'));
            $before_endDate = date('Y-m-d', strtotime($endDate.' -'.$diff_days.' days'));
        }
        return [
            'before_beginDate' => $before_beginDate,
            'before_endDate' => $before_endDate,
        ];
    }
}

if (!function_exists('issetAdd')) {
    function issetAdd(&$obj1, $obj2, $type = 'num')
    {
        if (!isset($obj1)) {
            $obj1 = $obj2;
        } else {
            if ($type == 'num') {
                $obj1 += $obj2;
            } else if ($type == 'arr') {
                $obj1 = array_merge($obj1, $obj2);
            }
        }
        return $obj1;
    }
}

if (!function_exists('forPrintArray')) {
    function forPrintArray($idArr)
    {
        $Arrids = '';
        foreach ($idArr as $idk => $idv) {
            $Arrids == '' ? $Arrids = (string)$idv : $Arrids .= ','.(string)$idv;
        }
        return $Arrids;
    }
}

if (!function_exists('addArrKey')) {
    function addArrKey(&$arr, $first, $second, $key, $value)
    {
        isset($arr[$first][$key]) ? $arr[$first][$key]++ : '';
        isset($arr[$first][$key.'_detail']) ? $arr[$first][$key.'_detail'][] = $value : '';
        isset($arr[$first]['channel'][$second][$key]) ? $arr[$first]['channel'][$second][$key]++ : '';
        isset($arr[$first]['channel'][$second][$key.'_detail']) ? $arr[$first]['channel'][$second][$key.'_detail'][] = $value : '';
        isset($arr[$first][$key]) ? $arr[-1][$key]++ : '';
        isset($arr[$first][$key]) ? $arr[-1][$key.'_detail'][] = $value : '';
    }
}

if (!function_exists('addArrKeyInit')) {
    function addArrKeyInit(&$arr, $first, $second, $first_name, $second_name, $key, $value)
    {
        //数值
        !isset($arr[$first][$key]) ? $arr[$first][$key] = 0 : '';
        !isset($arr[$first][$key.'_detail']) ? $arr[$first][$key.'_detail'] = [] : '';
        !isset($arr[$first]['channel'][$second][$key]) ? $arr[$first]['channel'][$second][$key] = 0 : '';
        !isset($arr[$first]['channel'][$second][$key.'_detail']) ? $arr[$first]['channel'][$second][$key.'_detail'] = [] : '';
        !isset($arr[-1][$key]) ? $arr[-1][$key] = 0 : '';
        !isset($arr[-1][$key.'_detail']) ? $arr[-1][$key.'_detail'] = [] : '';
        !isset($arr[-1]['channel'][-1][$key]) ? $arr[-1]['channel'][-1][$key] = 0 : '';
        !isset($arr[-1]['channel'][-1][$key.'_detail']) ? $arr[-1]['channel'][-1][$key.'_detail'] = [] : '';
        //名称初始化
        !isset($arr[$first]['name']) ? $arr[$first]['name'] = $first_name : '';
        !isset($arr[$first]['channel'][$second]['name']) ? $arr[$first]['channel'][$second]['name'] = $second_name : '';
        !isset($arr[-1]['name']) ? $arr[-1]['name'] = '合计' : '';
        !isset($arr[-1]['channel'][-1]['name']) ? $arr[-1]['channel'][-1]['name'] = '合计' : '';
        $arr[$first][$key]++;
        $arr[$first][$key.'_detail'][] = $value;
        $arr[$first]['channel'][$second][$key]++;
        $arr[$first]['channel'][$second][$key.'_detail'][] = $value;
        $arr[-1][$key]++;
        $arr[-1][$key.'_detail'][] = $value;
        $arr[-1]['channel'][-1][$key]++;
        $arr[-1]['channel'][-1][$key.'_detail'][] = $value;
    }
}

if (!function_exists('tuomin')) {
    //手机号脱敏
    function tuomin($tel)
    {
        return substr_replace($tel, '****', 3, 4);
    }
}

//是否在日期中
if (!function_exists('cpDate')) {
    function ifInDate($date, $bt, $et)
    {
        if (strtotime($date) < strtotime($bt.' 00:00:00') || strtotime($date) > strtotime($et.' 23:59:59')) {
            return false;
        }
        return true;
    }
}

//获取字符串中的字符
if (!function_exists('extractString')) {
    function extractString($str, $s, $e)
    {
        $start = strpos($str, $s) + strlen($s);
        $end = strpos($str, $e);
        return substr($str, $start, $end - $start);
    }
}

//获取字符串中的数字
if (!function_exists('formStatusOptions')) {
    function formStatusOptions() {
        return [
        	1 => '正常',
        	0 => '关闭'
        ];
    }
}

if (!function_exists('formResultOptions')) {
    function formResultOptions() {
        return [
            0 => '控涨效果理想',
            1 => '控涨效果一般',
            2 => '控涨效果较差'
        ];
    }
}

if (!function_exists('formSexOptions')) {
    function formSexOptions() {
        return [
            0 => '男',
            1 => '女'
        ];
    }
}